home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsesn1_1.lha / AmiTCP_session-1.1 / c / AmiTCP_session.rexx next >
OS/2 REXX Batch file  |  1995-06-16  |  7KB  |  314 lines

  1. /*
  2.  * AmiTCP_session version 1.1
  3.  *
  4.  * Arexx script to dial a SLIP server, start AmiTCP, send mail, etc.
  5.  * Usage: AmiTCP_session.rexx
  6.  *
  7.  * Written by David Jameson (dave@freeside.thegap.com), 1994
  8.  * based on AmiTCP_dialup by K. Raquel Sanborn
  9.  */
  10.  
  11. arg arg1
  12.  
  13. if (arg1 ~= "") & (arg1 ~= nowait) then do
  14.     say 'Invalid argument ' arg1
  15.     exit 10
  16. end
  17.  
  18. /* Constants */
  19.  
  20. cr              = '0d'x
  21. hangup_str      = 'ATH'||cr
  22. phone_number    = 'PHONE'        /* Put telephone number here */
  23. username        = 'USERNAME'||cr        /* Put your username here */
  24. passwd          = 'PASSWORD'||cr         /* Put your password here */
  25. logfile     = 'ncomm:ncomm.log'    /* Put name of log-file here */
  26. nntpserver    = 'news.gpl.net'    /* Put name of NNTP server here */
  27. serial_speed    = 57600            /* Put speed of serial port here */
  28. slip_device    = 'rhcslip.device'    /* Name of slip device */
  29. active_file    = 'uulib:active'    /* Name of active file */
  30. dialstr         = 'ATDT'||phone_number||cr
  31.  
  32.  
  33. /* Open libraries */
  34.  
  35. if ~show('L', 'rexxsupport.library') then do
  36.     if addlib('rexxsupport.library', 0, -30, 0) then
  37.     say 'added rexxsupport.library'
  38.     else do
  39.     say 'failed to open rexxsupport.library'
  40.     exit 10
  41.     end
  42. end
  43.  
  44. if ~show('L', 'rexxserdev.library') then do
  45.     if addlib('rexxserdev.library', 0, -30, 5) then
  46.     say 'added rexxserdev.library'
  47.     else do
  48.     say 'failed to open rexxserdev.library'
  49.     exit 10
  50.     end
  51. end
  52.  
  53. say 'Starting AmiTCP...'
  54. address command
  55. 'amitcp:bin/startnet'
  56.  
  57. serhandle = SerOpen('serial.device', 0)
  58. if serhandle = 0 then do
  59.     say 'Failed to open serial device'
  60.     exit 1
  61. end
  62.  
  63. check     = SerClear(serhandle)
  64. check     = SerSetParms(serhandle, serial_speed, 8, N, 1, 0 )
  65.  
  66. buffer = allocmem(4096)
  67. junk   = c2d(buffer)
  68.  
  69. /* Clean up when ctrl-C pressed */
  70. signal on break_c
  71.  
  72. say 'Dialing the terminal server... '
  73. check    = 0
  74. do forever
  75.     if (check == 0) then
  76.     do
  77.     result = SerWrite(serhandle, dialstr, length(dialstr))
  78.     check = 1
  79.     totalstr = ''
  80.     end
  81.     status   = SerQuery(serhandle)
  82.     parse upper var status valid bytes_read status_bits .
  83.     rcvdstr  = SerRead(serhandle, junk, bytes_read)
  84.     check = writech(STDOUT, rcvdstr)
  85.     totalstr = totalstr||rcvdstr
  86.     check    = modem_query(totalstr)
  87.     if (check == 100) then leave
  88.     result   = delay(100)
  89. end
  90.  
  91. totalstr = ''
  92.  
  93. check = SerFlush(serhandle, 'R')
  94. say 'Modem has connected to the terminal server...'
  95.  
  96. say 'Starting smtpd...'
  97. 'run smtpd'
  98.  
  99. check = 0
  100. count2 = 0
  101. do until check = 1
  102.     say 'Sending CR to server...'
  103.     check = SerWrite(serhandle, cr, length(cr))
  104.     check = wait_str(':')
  105.     if (check ~= 1) then do
  106.     count2 = count2 + 1
  107.     if (count2 > 5) then do
  108.         say 'Server not responding'
  109.         call hangup
  110.         call break_c
  111.     end
  112.     end
  113. end
  114.  
  115. say 'Received user prompt...'
  116. check = SerWrite(serhandle, username, length(username))
  117. rcvdstr = SerRead(serhandle, junk, length(username))
  118. check = writech(STDOUT, rcvdstr)
  119. say 'Sent Username, now waiting for password prompt...'
  120.  
  121. check = 0
  122. do until check = 1
  123.     check = wait_str(':')
  124. end
  125. say 'Received password prompt...'
  126. check = SerWrite(serhandle, passwd, length(passwd))
  127. say 'Sent password...'
  128.  
  129. check = SerClose(serhandle)
  130. serhandle = 0        /* Just in case ctrl-C is pressed */
  131. check = freemem(buffer,4096)
  132. buffer = 0
  133.  
  134. say 'Putting SLIP online'
  135. 'online' slip_device '0'
  136.  
  137. /* First try to open existing log file */
  138. check = open('log', logfile, 'a')
  139.  
  140. /* That didn't work, so now create a new log file */
  141. if (check == 0) then do
  142.     check = open('log', logfile, 'w')
  143.     if (check == 0) then do
  144.     say "Failed to open log file"
  145.     exit
  146.     end
  147. end
  148.  
  149. say 'Logging start of call...'
  150. check = writeln('log', 'AMITCP');
  151. check = writeln('log', '--------------------------------');
  152. weekday = left(date(weekday), 3)
  153. date = date()
  154. parse var date day month year
  155. time1 = time()
  156. parse var time1 hour1 ':' min1 ':' sec1
  157. check = writeln('log', 'Login: ' weekday month day||' '||time1||year);
  158.  
  159. say 'Sending outgoing mail (if any)...'
  160. 'breakname smtpd f'
  161.  
  162. say 'Sending outgoing news (if any)...'
  163. 'nntpsend'
  164.  
  165. say 'Beginning nntp transfer...'
  166. 'nntpxfer' nntpserver active_file 'paranoid'
  167.  
  168. if arg1 ~= nowait then do
  169.     /* Wait until user clicks on the 'OK' button */
  170.     'RequestChoice "AmiTCP Front-end" "Click to close AmiTCP session" QUIT >nil:'
  171. end
  172.  
  173. say 'Killing AmiTCP...'
  174. 'offline' slip_device '0'    /* Just in case AmiTCP doesn't get killed */
  175.  
  176. address AMITCP
  177. 'KILL'
  178. check = delay(150)
  179.  
  180. say 'Closing connection...'
  181.  
  182. serhandle = SerOpen('serial.device', 0)
  183. check     = SerClear(serhandle)
  184. check     = SerSetParms(serhandle, serial_speed, 8, N, 1, 0)
  185.  
  186. call hangup
  187.  
  188. check = SerClose(serhandle)
  189. serhandle = 0
  190.  
  191. say 'Logging end of call...'
  192.  
  193. time2 = time()
  194. date = date()
  195. weekday = left(date(week), 3)
  196. parse var date day month year
  197. check = writeln('log', 'Logout:' weekday month day||' '||time2||year);
  198. parse var time2 hour2 ':' min2 ':' sec2
  199.  
  200. /* Calculate time online */
  201. diffsecs = sec2 - sec1
  202. diffmins = min2 - min1
  203. diffhours = hour2 - hour1
  204. if diffsecs < 0 then do
  205.     diffsecs = diffsecs + 60
  206.     diffmins = diffmins - 1
  207. end
  208. if diffmins < 0 then do
  209.     diffmins = diffmins + 60
  210.     diffhours = diffhours - 1
  211. end
  212. if diffhours < 0 then do
  213.     diffhours = diffhours + 24
  214. end
  215.  
  216. /* Pad out digits with leading 0s */
  217. diffsecs = right(diffsecs, 2, '0')
  218. diffmins = right(diffmins, 2, '0')
  219. diffhours = right(diffhours, 2, '0')
  220.  
  221. check = writeln('log', 'Time online: 'diffhours||':'||diffmins||':'||diffsecs);
  222. check = writeln('log', '')
  223. check = close('log')
  224.  
  225. say 'Done!'
  226.  
  227. exit 0
  228.  
  229.  
  230. /*
  231.  * Procedure to determine the current status of the modem
  232.  */
  233. modem_query: procedure
  234.  
  235.     teststr = arg(1)
  236.     connectstr       = 'CONNECT'
  237.     busystr          = 'BUSY'
  238.     nocarrierstr     = 'NO CARRIER'
  239.  
  240.     test = pos(connectstr, teststr)
  241.     if (test ~= 0) then return 100
  242.  
  243.     test = pos(busystr, teststr)
  244.     if (test ~= 0) then return 0
  245.  
  246.     test = pos(nocarrierstr, teststr)
  247.     if (test ~= 0) then return 0
  248.  
  249.     return 200
  250.  
  251.  
  252. /*
  253.  * Subroutine to wait until a string is received from the serial port.
  254.  * Returns 1 if the string is found, otherwise returns 0
  255.  */
  256. wait_str:
  257.  
  258.     teststr = arg(1)
  259.     check = 0
  260.     count = 0
  261.     do until ((check ~= 0) | (count > 5))
  262.         status = SerQuery(serhandle)
  263.         parse upper var status valid bytes_read status_bits .
  264.         rcvdstr = SerRead(serhandle, junk, bytes_read)
  265.     check   = writech(STDOUT, rcvdstr)
  266.         check   = pos(teststr,rcvdstr)
  267.         test    = delay(50)
  268.     count = count + 1
  269.     end
  270.     if (count > 5) then return 0
  271. return 1
  272.  
  273. /*
  274.  * Subroutine to hang up the modem
  275.  */
  276. hangup:
  277.  
  278. /* Send +++, wait the send ATH, then exit */
  279.  
  280. say 'Sending +++'
  281. check = delay(75)
  282. check = SerWrite(serhandle, '+++', length('+++'))
  283. check = delay(75)
  284. say 'Sending ATH<cr>'
  285. check = SerWrite(serhandle, 'A', 1)
  286. check = delay(2)
  287. check = SerWrite(serhandle, 'T', 1)
  288. check = delay(2)
  289. check = SerWrite(serhandle, 'H', 1)
  290. check = delay(2)
  291. check = SerWrite(serhandle, cr, 1)
  292. check = delay(25)
  293. return
  294.  
  295. /*
  296.  * Ctrl-C signal handling routine.
  297.  */
  298. break_c:
  299.     say 'Cleaning up...'
  300.     if (serhandle ~= 0) then
  301.     check = SerClose(serhandle)
  302.  
  303.     if (buffer ~= 0) then
  304.     check = freemem(buffer, 4096)
  305.  
  306.     address command
  307.     'offline' slip_device '0'    /* Just in case AmiTCP doesn't get killed */
  308.  
  309.     address AMITCP
  310.     'KILL'
  311.     check = delay(150)
  312. exit 20
  313.  
  314.